﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Technullogy
{
    public static class TechnullogyConfiguration
    {
        public static String LocalDirectory
        {
            get
            {
                return GetString("LocalDirectory", String.Empty);
            }
        }

        public static String RemoteDirectory
        {
            get
            {
                return GetString("RemoteDirectory", String.Empty);
            }
        }

        public static bool CopyLocalToRemote
        {
            get
            {
                String boolValue = GetString("CopyLocalToRemote", "true");
                return ConvertToBool(boolValue, true);
            }
        }

        public static bool CopyRemoteToLocal
        {
            get
            {
                String boolValue = GetString("CopyRemoteToLocal", "true");
                return ConvertToBool(boolValue, true);
            }
        }

        public static bool DeleteLocalEmptyDirectories
        {
            get
            {
                String boolValue = GetString("DeleteLocalEmptyDirectories", "false");
                return ConvertToBool(boolValue, false);
            }
        }

        public static bool DeleteRemoteEmptyDirectories
        {
            get
            {
                String boolValue = GetString("DeleteRemoteEmptyDirectories", "false");
                return ConvertToBool(boolValue, false);
            }
        }

        private static String _fileTypesToExclude
        {
            get
            {
                return GetString("FileTypesToExclude", String.Empty);
            }
        }
        public static String[] FileTypesToExclude
        {
            get
            {
                return _fileTypesToExclude.Split(',');
            }
        }

        private static String _fileTypesToDelete
        {
            get
            {
                return GetString("FileTypesToDelete", String.Empty);
            }
        }
        public static String[] FileTypesToDelete
        {
            get
            {
                return _fileTypesToDelete.Split(',');
            }
        }

        public static String SyncLogFileLocation
        {
            get
            {
                return GetString("SyncLogFileLocation", @"C:\Technullogy DirectorySync.log");
            }
        }

        public static String EmptyDirectoryLogFileLocation
        {
            get
            {
                return GetString("EmptyDirectoryLogFileLocation", @"C:\Technullogy EmptyDirectoryDelete.log");
            }
        }

        public static bool EnableVerboseLogging
        {
            get 
            {
                String boolValue = GetString("EnableVerboseLogging", "false");
                return ConvertToBool(boolValue, false); 
            }
        }

        public static bool AppendLogFile
        {
            get
            {
                String boolValue = GetString("AppendLogFile", "true");
                return ConvertToBool(boolValue, true);
            }
        }

        public static bool OverwriteDestinationIfSourceIsLarger
        {
            get
            {
                String boolValue = GetString("OverwriteDestinationIfSourceIsLarger", "false");
                return ConvertToBool(boolValue, false); 
            }
        }

        /// <summary>Returns a string setting and logs it if it's missing
        /// </summary>
        /// <param name="name">Name of the setting to retrieve</param>
        /// <param name="defaultValue">Sets string to this value if a configuration key is not found</param>
        /// <returns></returns>
        public static String GetString(String name, String defaultValue)
        {
            string value = ConfigurationManager.AppSettings.Get(name);

            if (String.IsNullOrEmpty(value))
                return defaultValue;

            return value;
        }

        /// <summary>Coverts a string to a boolean value
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool ConvertToBool(String value, bool defaultValue)
        {
            if (String.IsNullOrEmpty(value))
                return defaultValue;

            String firstChar = value.ToLower().Substring(0, 1);

            switch (firstChar)
            {
                case "t":
                case "1":
                case "y":
                    return true;
                case "f":
                case "0":
                case "n":
                    return false;
                default:
                    return false;
            }
        }
    }
}
